﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AllenData
{
    public class HomeOwnerStatusData
    {
        public HomeOwnerStatus GetHomeOwnerStatus(int homeOwnerStatusId)
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                var homeOwnerStatus = ctx.HomeOwnerStatuses.Where(d => d.HomeOwnerStatusID == homeOwnerStatusId);
                return homeOwnerStatus.FirstOrDefault();
            }
        }

        public List<HomeOwnerStatus> GetAllhomeOwnerStatuses()
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                return ctx.HomeOwnerStatuses.ToList();
            }
        }

        public int AddNewHomeOwnerStatus(HomeOwnerStatus homeOwnerStatus)
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                ctx.AddToHomeOwnerStatuses(homeOwnerStatus);
                ctx.SaveChanges();

                HomeOwnerStatus newHomeOwnerStatus = ctx.HomeOwnerStatuses.Where(
                    d => d.Name == homeOwnerStatus.Name).FirstOrDefault();

                if (newHomeOwnerStatus == null)
                    return -1;
                else
                    return newHomeOwnerStatus.HomeOwnerStatusID;
            }
        }

        public void UpdateHomeOwnerStatus(HomeOwnerStatus homeOwnerStatus)
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                HomeOwnerStatus dbDept = ctx.HomeOwnerStatuses.First(d => d.HomeOwnerStatusID == homeOwnerStatus.HomeOwnerStatusID);
                ctx.ApplyCurrentValues("homeOwnerStatuses", homeOwnerStatus);
                ctx.SaveChanges();
            }
        }

        public void DeleteHomeOwnerStatus(int homeOwnerStatusId)
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                HomeOwnerStatus dbDept = ctx.HomeOwnerStatuses.First(d => d.HomeOwnerStatusID == homeOwnerStatusId);
                ctx.DeleteObject(dbDept);
                ctx.SaveChanges();
            }
        }
    }
}
